﻿using Appliaction.AppServices.Inv;
using Appliaction.Dto.Inv;
using Appliaction.IAppServices.Inv;
using AutoMapper;
using Domain.Entitys.Inv;
using Domain.IRepositorys.Inv;
using EFCore.Repositorys.Inv;
using Microsoft.AspNetCore.Mvc;
using static StackExchange.Redis.Role;

namespace API.Controllers.Inv
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class Inv_StockController : Controller
    {
        private readonly IMapper _mapper;
        private readonly IInv_StockServices  _iiv_StockServices;


        public Inv_StockController(IInv_StockServices _iiv_StockServices, IMapper mapper)
        {
            this._iiv_StockServices = _iiv_StockServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 查询仓库库存
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <param name="zone_code"></param>
        /// <param name="location_code"></param>
        /// <param name="por_code"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<int> GetinvStock(string weahouse_code,  string por_code) { 
        
        return _iiv_StockServices.Getinv_stock(weahouse_code,  por_code);
        }

        /// <summary>
        /// 根据仓库查询库存表里面的数据进行筛选产品
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<JsonResult> Get_Select_weahouse_code(string weahouse_code, int currentPage, int pageSize)
        {
           return new JsonResult(await _iiv_StockServices.Get_Select_weahouse_code(weahouse_code,currentPage,pageSize));
        }


        /// <summary>
        /// 查询仓库内商品数量
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<int> Get_Select_Weahouse_code_Count(string weahouse_code)
        {
            return _iiv_StockServices.Get_Select_Weahouse_code_Count(weahouse_code);
        }

        /// <summary>
        /// 根据仓库查询库存表里面的数据进行筛选产品--仓库库存模块
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> Get_Select_stock(string ? weahouse_code, string ? zone_code, string ? location_code, string ? Vendor_Code, string ? pro_code, int currentPage, int pageSize)
        {
            return new JsonResult(await _iiv_StockServices.Get_Select_stock(weahouse_code, zone_code, location_code, Vendor_Code, pro_code, currentPage, pageSize));
            
        }


        /// <summary>
        /// 查询仓库表的数据--仓库库存模块
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<int> Get_Select_stock_count(string ? weahouse_code, string ? zone_code, string ? location_code, string ? Vendor_Code, string ? pro_code)
        {

           return _iiv_StockServices.Get_Select_stock_count(weahouse_code, zone_code, location_code, Vendor_Code, pro_code);
        }


        /// <summary>
        /// 盘亏--修正库存
        /// </summary>
        /// <param name="inv_stock"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<IActionResult> Update_inv_Stock_numder([FromBody] List<Inv_Stock_numderUpdateDto> inv_stock)
        {
            try { 
            int reslt = await _iiv_StockServices.Update_inv_Stock_numder(inv_stock);
            return Ok(reslt);
            }catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }


        /// <summary>
        /// 根据仓库查询库存表里面的数据进行筛选产品--添加产品编码和字典编码的条件
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <param name="pro_code"></param>
        /// <param name="dict_code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> Get_Select_stock_All(string  weahouse_code, string? pro_code, int dict_code, int currentPage, int pageSize)
        {

            return  new JsonResult(await _iiv_StockServices.Get_Select_stock_All(weahouse_code, pro_code, dict_code, currentPage, pageSize));
        }


        /// <summary>
        /// 查询库存里面的数据总数--添加二个条件 产品码编码
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<int> Get_Select__stock_Count(string weahouse_code, string? pro_code, int dict_code)
        {

            return _iiv_StockServices.Get_Select__stock_Count(weahouse_code, pro_code, dict_code);
        }


        /// <summary>
        /// 修改库存调拨 
        /// </summary>
        /// <param name="Stock_UpdateTrfDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public  Task<int> stock_Update_kc(List<Stock_UpdateTrfDto> Stock_UpdateTrfDto)
        {
            return  _iiv_StockServices.stock_Update_kc(Stock_UpdateTrfDto);
        }


        /// <summary>
        /// 根据查询库存--详情页面
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <param name="por_code"></param>
        /// <param name="stock_entry_no"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<int> Getinv_stock_XQ(string weahouse_code, string por_code, string stock_entry_no)
        {
            return _iiv_StockServices.Getinv_stock_XQ(weahouse_code, por_code, stock_entry_no);
        }

        /// <summary>
        /// 获取商品所有下面的产品库存--产品编码
        /// </summary>
        /// <param name="Pro_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<int> Get_inv_stock_totalnumber(string Pro_code)
        {
            return _iiv_StockServices.Get_inv_stock_totalnumber(Pro_code);
        }


        /// <summary>
        /// 结构入库编号,产品编号,仓库编号去查询这个产品在的库位和库区
        /// </summary>
        /// <param name="weahouse_code"></param>
        /// <param name="pro_code"></param>
        /// <param name="stock_entry_no"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<JsonResult> Get_Select_stock_All_By_Pro_Code(string weahouse_code, string pro_code, string stock_entry_no)
        {
            return Json(await _iiv_StockServices.Get_Select_stock_All_By_Pro_Code(weahouse_code, pro_code, stock_entry_no));
        }

        /// <summary>
        /// 减少库存
        /// </summary>
        /// <param name="inv_StockothoutordUpdateDtos"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Stock_update([FromBody] List<Inv_stockothoutordUpdateDto> inv_StockothoutordUpdateDtos) {
        
        int reslt = await _iiv_StockServices.Stock_update(inv_StockothoutordUpdateDtos);
            return Ok(new { succes = true, count = reslt });
        }



        /// <summary>
        /// 选择期初入库的时候--有动态的展示库存数量
        /// </summary>
        /// <param name="warehouse_code"></param>
        /// <param name="zone_code"></param>
        /// <param name="location_code"></param>
        /// <param name="pro_code"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<decimal> int_stock_numder(string warehouse_code, string zone_code, string location_code, string pro_code)
        {

            return _iiv_StockServices.int_stock_numder(warehouse_code, zone_code, location_code, pro_code);
        }


        /// <summary>
        /// 修改库存的数据不合格数量
        /// </summary>
        /// <param name="prodInfo_totnum_uqual"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        //[HttpPut]
        //public async  Task<IActionResult> Stock_Update_stockUnqualified_numder(List<Inv_StockUpdateDto> prodInfo_totnum_uqual)
        //{
        //  int reslt = await _iiv_StockServices.stock_Update_stockUnqualified_numder(prodInfo_totnum_uqual);
        //    return Ok(reslt);
        //}
    }
}
